package org.excel;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) throws Exception {
        String sourceFileUrl = "/Users/zhaoweijie/excel/万里公司预招录船员信息采集(汇总表).xlsx";
        String imageOutputDir = "/Users/zhaoweijie/excel/万里公司预招录船员信息采集(汇总表)_img/";
        // 创建监听器
        WPSExcelImportImgListener listener = new WPSExcelImportImgListener();
        // 获取文件输入流
        InputStream inputStreamForData = new FileInputStream(sourceFileUrl);
        // 使用 EasyExcel 读取数据
        List<ExcelData> excelList = EasyExcel.read(inputStreamForData, ExcelData.class, listener).sheet().doReadSync();
        // 重置输入流以供图片提取
        InputStream inputStreamForImages = new FileInputStream(sourceFileUrl);
        // 提取图片并保存
        WPSExcelImportImgExtractor extractor = new WPSExcelImportImgExtractor();
        Map<String, String> cellImageMap = extractor.extractImages(inputStreamForImages, imageOutputDir);
        System.out.println("文件map" + JSON.toJSONString(cellImageMap));
        transferExcelData(excelList,cellImageMap,"AQ");
        System.out.println("================数据劫持转换=====================");
        System.out.println("list"+excelList);
    }

    /**
     * 读取excel,放入List<Map<String, String>>
     *
     * @param fileName  读取excel的文件名称
     * @param sheetName
     * @return datalist
     */
    public static List<Map<String, Object>> readExcelToMap(String fileName) {
        List<Map<String, Object>> dataList = new ArrayList<>();
        EasyExcel.read(fileName, new AnalysisEventListener<Map<String, Object>>() {

            //直接使用Map来保存数据
            @Override
            public void invoke(Map<String, Object> data, AnalysisContext context) {
                //读取到的每行数据,其key是以0开始的索引
                dataList.add(data);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                System.out.println(("Excel读取完成,文件名:" + fileName + ",行数：" + dataList.size()));

            }
        }).sheet().doRead();
        return dataList;
    }

    /**
     * 文件列数据转换
     * @param list
     * @param fileMap
     * @param columnName A/AB
     */
    public static void transferExcelData(List<ExcelData> list, Map<String, String> fileMap, String... columnName) throws NoSuchFieldException, IllegalAccessException {
        List<String> arrList = new ArrayList(fileMap.keySet());
        List<String> colList= new ArrayList(Arrays.asList(columnName));
        String fieldPrefix = "col";
        for(ExcelData item : list){
            for (String column :colList) {
                 Field field = item.getClass().getDeclaredField(fieldPrefix+column);
                 field.setAccessible(true);
                 String fieldValue = (String) field.get(item);
                 List<String> collect = arrList.stream().filter(v -> fieldValue.indexOf(v) != -1).collect(Collectors.toList());
                 if(collect!=null && collect.size()>0){
                     field.set(item,fileMap.get(collect.get(0)));
                 }
                System.out.println(fieldValue);
            }
        }
    }

}
